package com.rqf.ls.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.ls.service.ICommonDataService;
import com.rqf.ls.service.IOrderService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("OrderService")
public class OrderService implements IOrderService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	/**
	 * 新增预约
	 * @param input
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月29日
	 */
	@Override
	public Boolean add(Map<String, Object> input){
		String sql = "insert into yuyue (id,yuyuetime,able,whoyuyueid,fuwusmallclassid,whoyuyueaddress,content) values(yuyue_sequence.nextval,'%s','1','%s','%s','%s','%s')";
		String format = String.format(sql, input.get("yuyuetime"),input.get("whoyuyueid"),input.get("fuwusmallclassid"),input.get("whoyuyueaddress"),input.get("content"));
		Boolean isSuccess = true;
		try {
			jdbcTemplate.execute(format);
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	@Override
	public Boolean update(Integer orderId,Map<String, Object> inputMap){
		String sql = "Update yuyue set %s where id=%s";
		StringBuffer updateStr = new StringBuffer();
		Set<String> keySet = inputMap.keySet();
		for(String key : keySet){
			if (updateStr.length() > 0) {
				updateStr.append(",");
			}
			updateStr.append(key).append("='").append(inputMap.get(key)).append("'");
		}
		
		String format = String.format(sql, updateStr.toString(),orderId);
		Boolean isSuccess = true;
		try {
			jdbcTemplate.execute(format);
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	@Override
	public Boolean delete(Integer id){
		String sql = "Delete from yuyue where id=%s";
		String format = String.format(sql,id);
		Boolean isSuccess = true;
		try {
			jdbcTemplate.execute(format);
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	 
	 /**
	 * 预约列表
	 * @param id
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月29日
	 */
	@Override
	public List<Map<String, Object>> getList() {
		String sql = "Select y.*,l.username from yuyue y left outer join lsuser l on l.phone=y.whoyuyueid where (y.yuyuewhoid is null or y.yuyuewhoid='') order by y.yuyuetime desc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	@Override
	public DataVector<Map<String, Object>> getList(String phoneNo,String lawyerNo,RequestInput requestInput) {
		String table = "V_YUYUE";
		String _whereStr = "";
		if (!StringUtils.isEmpty(phoneNo)) {
			_whereStr = "whoyuyueid='"+phoneNo+"'";
		}
		if (!StringUtils.isEmpty(lawyerNo)) {
			if (_whereStr.length() > 0) {
				_whereStr += " and ";
			}
			_whereStr = "yuyuewhoid='"+lawyerNo+"'";
		}else{
			if (_whereStr.length() > 0) {
				_whereStr += " and ";
			}
			_whereStr = "yuyuewhoid is null or yuyuewhoid=''";
		}
		String sortStr = "yuyuetime desc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(table, _whereStr, sortStr, requestInput);
		return listData;
	}
	@Override
	public List<Map<String, Object>> getList(String phoneNo) {
		String sql = "Select y.*,l.username from yuyue y left outer join lsuser l on l.phone=y.whoyuyueid where y.whoyuyueid='%s' order by y.yuyuetime desc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, phoneNo));
		return list;
	}
	@Override
	public Map<String, Object> getForm(Integer orderId) {
		String sql = "Select y.*,l.username from yuyue y left outer join lsuser l on l.phone=y.whoyuyueid where y.id=%s";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, orderId));
		if (list.size() > 0) {
			return list.get(0);
		}else {
			return new HashMap<String, Object>();
		}
	}
	
	
}
